package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttest.common.beans.PaginationResult;
import com.smarttest.common.beans.StatResult;
import com.smarttest.portal.mapper.CheckinterfaceMapper;
import com.smarttest.portal.pojo.Checkinterface;
import com.smarttest.portal.pojo.CheckinterfaceExample;
import com.smarttest.portal.pojo.CheckinterfaceExample.Criteria;
import com.smarttest.portal.service.CheckInterfaceService;

@Service
public class CheckInterfaceServiceImpl implements CheckInterfaceService{
	 @Autowired
		private CheckinterfaceMapper checkinterfaceMapper;
		@Override
		public int insert(Checkinterface checkinterface) {
			return checkinterfaceMapper.insertSelective(checkinterface);
		}
		@Override
		public PaginationResult getCheckinterList(int page, int rows) {
			// TODO Auto-generated method stub
			CheckinterfaceExample example = new CheckinterfaceExample();
			//分页处理
			PageHelper.startPage(page, rows);
			List<Checkinterface> list = checkinterfaceMapper.selectByExample(example);
			//创建一个返回值对象
			PaginationResult result = new PaginationResult();
			result.setRows(list);
			//取记录总条数
			PageInfo<Checkinterface> pageInfo = new PageInfo<>(list);
			result.setTotal(pageInfo.getTotal());
			return result;
		}
		@Override
		public StatResult getStatResult(Date start, Date end) {
			CheckinterfaceExample example = new CheckinterfaceExample();
			Criteria criteria = example.createCriteria();
			example.setGroupbyClause("method,code");
			criteria.andCreatetimeBetween(start, end);
			
			List<Checkinterface> list = checkinterfaceMapper.selectSumByExample(example);
			
			StatResult result = new StatResult();
			
			
			if (list == null){
				result.setResult(-1);
			}else{
				result.setResult(0);
				result.setContent(list);
			}
			return result;
		}
		@Override
		public PaginationResult getCheckinterListByTime(Date start, Date end,
				String orderBy, int page, int rows) {
			// TODO Auto-generated method stub
			
			CheckinterfaceExample example = new CheckinterfaceExample();
			example.setOrderByClause(orderBy+" ASC");
			Criteria criteria = example.createCriteria();
			criteria.andCreatetimeBetween(start, end);
			PageHelper.startPage(page, rows);
			List<Checkinterface> list = checkinterfaceMapper.selectByExample(example);
			
			
			//创建一个返回值对象
			PaginationResult result = new PaginationResult();
			result.setRows(list);
			//取记录总条数
			PageInfo<Checkinterface> pageInfo = new PageInfo<>(list);
			result.setTotal(pageInfo.getTotal());
			return result;
		}
}
